package com.gxa.dr2.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.dr2.dto.LayDTO;
import com.gxa.dr2.dto.Response;
import com.gxa.dr2.dto.ResultDTO;
import com.gxa.dr2.mapper.CustomerMapper;
import com.gxa.dr2.param.MyParam;
import com.gxa.dr2.pojo.Plan;
import com.gxa.dr2.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;

/**
 * @program: customer_relation_manage
 * @description:
 **/

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public LayDTO list(MyParam param) {
        // 分页
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<Plan> plans = customerMapper.findByParam(param);
        // 将结果集包装成pageInfo
        PageInfo<Plan> pageInfo = new PageInfo<>(plans);

        return Response.success(pageInfo.getTotal(), pageInfo.getList());

    }



    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ResultDTO add(Plan plan) {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        plan.setCreateTime(timestamp);
        plan.setUpdateTime(timestamp);
        this.customerMapper.save(plan);


        return Response.success("添加成功！");
    }



    @Override
    public ResultDTO detail(Integer planId) {

        Plan plan = this.customerMapper.findById(planId);
        return Response.success(plan);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ResultDTO edit(Plan plan) {

        plan.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        this.customerMapper.update(plan);
        return Response.success("修改成功！");
    }





    @Override
    public ResultDTO deleteFalse(Integer planId) {
        this.customerMapper.deleteFalse(planId);
        return Response.success("删除成功！");
    }

    @Override
    public ResultDTO deleteAll(Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        // 直接删除
        this.customerMapper.deleteAll(ids);
        return Response.success("批量删除成功！");
    }
}
